<style lang="less" scoped>
#box {
  overflow: auto;
  height: calc(100vh - 195px);
}

/deep/ .el-tabs__content {
  overflow: auto;
}

.el-tab-pane {
  height: 300px;
  overflow: auto;
}

.remote-consultation {
  padding-left: 20px;
  margin-top: 20px;
  border-bottom: 1px solid #e5e5e5;

  &_item {
    width: 30%;
  }

  &_button {
    width: 100%;
    text-align: center;
    margin-top: 100px;
  }
}

.consultation-doctorname {
  width: 200px;
  margin-top: 20px;
  display: inline-flex;
  padding: 0 10px 0 0;
  justify-content: space-between;

  .consultation-image {
    display: flex;

    .doctor-info {
      line-height: 30px;
    }
  }
}
</style>

<template>
  <div id="box">
    <el-form class="remote-consultation">
      <el-form-item v-if="consultationsTime" label="会诊时间：">
        {{ consultationsTime }}
      </el-form-item>
      <el-form-item label="会诊说明：">
        <el-input
          v-model="consExplain"
          type="textarea"
          :autosize="{ minRows: 2, maxRows: 4 }"
          style="width: calc(100% - 300px)"
        />
      </el-form-item>
      <el-form-item label="会诊专家：">
        <el-tag v-for="(tag, index) in tags" :key="index" style="margin-right: 10px">
          {{ tag.departmentName }}-{{ tag.username }}
        </el-tag>
        <el-button
          type="primary"
          size="mini"
          plain
          icon="el-icon-plus"
          style="margin-left: 10px"
          @click="inviteExperts"
        >邀请专家</el-button>
      </el-form-item>
    </el-form>
    <el-form class="remote-consultation" inline>
      <el-form-item label="姓名：" class="remote-consultation_item">
        {{ userForm.name }}
      </el-form-item>
      <el-form-item label="身份证号：" class="remote-consultation_item">
        {{ userForm.idCard }}
      </el-form-item>
      <el-form-item label="年龄：" class="remote-consultation_item">
        {{ userForm.age }}
      </el-form-item>
      <el-form-item label="性别：" class="remote-consultation_item">
        {{ userForm.sex }}
      </el-form-item>
      <el-form-item label="手机号码：" class="remote-consultation_item">
        {{ userForm.tel }}
      </el-form-item>
      <el-form-item label="发病时间：" class="remote-consultation_item">
        {{ userForm.diseaseTime }}
      </el-form-item>
      <!-- td-start -->
      <div v-if="diseaseType === '创伤'">
        <el-form-item label="MPDS：" class="remote-consultation_item">
          {{ mpds }}
        </el-form-item>
        <el-form-item label="入院诊断：" class="remote-consultation_item">
          {{ admittingDiagnosis }}
        </el-form-item>
        <el-form-item label="其他处置：" class="remote-consultation_item">
          {{ treatment }}
        </el-form-item>
        <el-form-item label="TI评分：" class="remote-consultation_item">
          {{ tiScore }}
        </el-form-item>
        <el-form-item label="GCS评分：" class="remote-consultation_item">
          {{ gcsScore }}
        </el-form-item>
        <el-form-item label="ISS、AIS评分：" class="remote-consultation_item">
          {{ iisScore }}
        </el-form-item>
      </div>
      <!-- td-end -->
    </el-form>
    <el-form class="remote-consultation" inline>
      <el-form-item label="主持医生：" class="remote-consultation_item">
        {{ surgeon }}
      </el-form-item>
      <el-form-item label="联系方式：" class="remote-consultation_item" />
      <el-form-item label="会议地点：" class="remote-consultation_item" />
    </el-form>
    <div class="remote-consultation_button">
      <el-button
        v-show="consultationsId"
        type="primary"
        @click="inviteCons"
      >邀请专家</el-button>
      <el-button type="primary" @click="startCons">{{
        consultationsId ? "进入会诊" : "开启会诊"
      }}</el-button>
      <el-button
        v-show="consultationsId"
        type="danger"
        @click="endCons"
      >结束会诊</el-button>
    </div>
    <el-dialog title="会诊专家列表" :visible.sync="dialogVisible" width="1200px">
      <el-tabs tab-position="left">
        <el-tab-pane :label="department">
          <div
            v-for="(item, index) in doctorList"
            :key="index"
            class="consultation-doctorname"
          >
            <div class="consultation-image">
              <img
                src="../../images/doctor.png"
                alt=""
                style="width: 60px; height: 60px; margin-right: 10px"
              >
              <div class="doctor-info">
                <div>{{ item.authority }}</div>
                <div>{{ item.username }}</div>
              </div>
            </div>
            <el-checkbox v-model="item.checked" style="line-height: 60px" />
          </div>
        </el-tab-pane>
      </el-tabs>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitDoctor">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  getConUserList,
  selectConsultations,
  startConsultations,
  forceEndConsultations,
  inviteConsultations,
  acceptConsultations
} from '@/api/green-channel/remoteConsultation'
import { findDiagnoses } from '@/api/green-channel/assessmentTrauma'
import { firstTouchPoints } from '@/api/green-channel/getAllEvent'
import { queryListDisGet } from '@/api/green-channel/otherDisposal'
import { getPatientInfo } from '@/api/green-channel/info'
export default {
  name: 'RemoteConsultation',
  // eslint-disable-next-line vue/require-prop-types
  props: ['checkrowdata', 'patientType'],
  data() {
    return {
      consultationsTime: '',
      consultationsId: null,
      userRoles: null,
      consultationsStatus: null,
      consTime: '',
      meetPrefix: '',
      consExplain: '',
      userForm: {
        name: '',
        idCard: '',
        age: '',
        sex: '',
        tel: '',
        diseaseTime: ''
      },
      surgeon: '',
      dialogVisible: false,
      tags: [],
      department: '',
      doctorList: [],
      gcsScore: 0,
      iisScore: 0,
      tiScore: 0,
      mpds: '',
      admittingDiagnosis: '',
      mpdsObj: {
        mpds0: '交通/运输事故',
        mpds1: '高处坠落',
        mpds2: '暴力袭击/侵犯',
        mpds3: '动物咬伤/攻击',
        mpds4: '烧伤(烫伤)爆炸伤',
        mpds5: '锐器刺伤/枪伤/贯穿伤',
        mpds6: '触电/雷击伤',
        mpds7: '冬季运动损伤',
        mpds8: '机械损伤',
        mpds9: '创伤(其他创伤)'
      },
      treatment: '',
      diseaseType: '创伤'
    }
  },
  mounted() {
    this.diseaseType = this.checkrowdata.diseaseType
    this.init()
  },
  methods: {
    init() {
      this.consultationsId = null
      this.consultationsStatus = null
      console.log(this.$store.getters.sysRole, 'getters')
      if (this.$store.getters.sysRole) {
        this.userRoles = JSON.parse(this.$store.getters.sysRole)[0]
      }
      this.meetPrefix = this.$store.getters.meetPrefix
      this.surgeon = this.$store.getters.name

      firstTouchPoints({
        apiToken: this.$store.getters.apiToken,
        dispatchId: this.checkrowdata.dispatchId
      }).then((res) => {
        if (res.code == 0) {
          if (res.data && res.data.length > 0) {
            res.data.forEach((el) => {
              if (el.event_id == '51810') {
                this.admittingDiagnosis = el.detail
              }
            })
          }
        }
      })
      findDiagnoses({
        dispatchId: this.checkrowdata.dispatchId
      }).then((res) => {
        if (res && res.length > 0) {
          res.forEach((el) => {
            if (el.diagnoseType == 'trauma_ti') {
              if (el.diagnoseContent) {
                this.tiScore = el.score
              }
            }
            if (el.diagnoseType == 'trauma_gcs') {
              if (el.diagnoseContent) {
                this.gcsScore = el.score
              }
            }
            if (el.diagnoseType == 'trauma_iss') {
              if (el.diagnoseContent) {
                this.iisScore = el.score
              }
            }
            if (el.diagnoseType == 'MPDS') {
              if (el.diagnoseContent) {
                this.mpds = JSON.parse(el.diagnoseContent)
                if (this.mpds.length > 0) {
                  let temp_data = ''
                  for (let i = 0; i < this.mpds.length; i++) {
                    temp_data +=
                      this.mpdsObj[this.mpds[i]] +
                      (i === this.mpds.length - 1 ? '' : '，')
                  }
                  this.mpds = temp_data
                }
              }
            }
          })
        }
      })
      selectConsultations({
        apiToken: this.$store.getters.apiToken,
        dispatchId: this.checkrowdata.dispatchId
          ? this.checkrowdata.dispatchId.toString()
          : '',
        consultationsId: ''
      }).then((res) => {
        if (res.code == 0) {
          if (res.data) {
            if (res.data.ConsultationsId && res.data.ConsultationsStatus != 3) {
              this.consultationsId = res.data.ConsultationsId
            }
            if (res.data.ConsultationsTime) {
              this.consultationsTime = res.data.ConsultationsTime
            }
            this.consultationsStatus = res.data.ConsultationsStatus
            this.userForm.name = res.data.name
            this.userForm.age = res.data.age
            this.userForm.sex = res.data.sex
            // this.userForm.diseaseTime = res.data.FIRST_MC_TIME
            this.userForm.diseaseTime = res.data.incidentDateTime
          }
        }
      })
      this.treatment = ''
      queryListDisGet({
        dispatchId: this.checkrowdata.dispatchId
      }).then((res) => {
        const dataArr = res.data
        for (let i = 0; i < dataArr.length; i++) {
          if (dataArr[i].datatype === 'treatment') {
            this.treatment +=
              JSON.parse(dataArr[i].treatment).join('，') +
              (i === dataArr.length - 1 ? '' : '，')
          }
        }
      })
      this.getPatientInfo()
    },
    // 邀请医生
    inviteExperts() {
      this.department = ''
      this.doctorList = []
      getConUserList({
        dispatchId: this.checkrowdata.dispatchId
          ? this.checkrowdata.dispatchId.toString()
          : '',
        departmentNameStr: '',
        role: this.userRoles,
        loggedIn: ''
      }).then((res) => {
        if (res.code == 0) {
          this.dialogVisible = true
          this.department = res.data.department1
          if (
            res.data.department1DoctorList &&
            res.data.department1DoctorList.length > 0
          ) {
            if (this.tags && this.tags.length > 0) {
              this.tags.forEach((tag) => {
                res.data.department1DoctorList.forEach((el) => {
                  el.checked = false
                  if (tag.username == el.username) {
                    el.checked = true
                  }
                })
              })
            }
            this.doctorList = res.data.department1DoctorList
          }
        }
      })
    },
    // 确定
    submitDoctor() {
      this.tags = []
      if (this.doctorList && this.doctorList.length > 0) {
        this.doctorList.forEach((el) => {
          if (el.checked) {
            this.tags.push(el)
          }
        })
      }
      this.dialogVisible = false
    },
    // 发起会诊
    startCons() {
      if (this.consultationsId) {
        acceptConsultations({
          apiToken: this.$store.getters.apiToken,
          consultationsId: this.consultationsId,
          screenid: 0,
          userIdStr: '',
          userId: this.$store.getters.userId
        }).then((res) => {
          if (res.code == 0) {
            window.open(this.meetPrefix + this.consultationsId)
          }
        })
      } else {
        if (this.tags && this.tags.length > 0) {
          const userid = []
          this.tags.forEach((el) => {
            userid.push(el.id)
          })
          startConsultations({
            dispatchid: this.checkrowdata.dispatchId
              ? this.checkrowdata.dispatchId.toString()
              : '',
            description: this.consExplain,
            meettype: '2',
            diseasetype: this.patientType,
            consultationtype: '视频会诊',
            fromuserid: this.$store.getters.userId,
            touserids: userid.length > 0 ? userid.join(',') : ''
          }).then((res) => {
            if (res.code == 0) {
              if (res.data) {
                window.open(this.meetPrefix + res.data)
              }
              this.init()
            } else {
              this.$message({
                type: 'error',
                message: res.error
              })
            }
          })
        } else {
          this.$message('请选择需要邀请的专家')
        }
      }
    },
    // 结束会诊
    endCons() {
      forceEndConsultations({
        apiToken: this.$store.getters.apiToken,
        consultationsId: this.consultationsId
      }).then((res) => {
        if (res.code == 0) {
          this.$message({
            type: 'success',
            message: '已结束会议'
          })
          this.init()
        }
      })
    },
    // 邀请专家
    inviteCons() {
      if (this.tags && this.tags.length > 0) {
        const userid = []
        this.tags.forEach((el) => {
          userid.push(el.id)
        })
        inviteConsultations({
          apiToken: this.$store.getters.apiToken,
          consultationsId: this.consultationsId,
          meettype: '2',
          userId: userid.length > 0 ? userid.join(',') : '',
          type: '',
          transmit: ''
        }).then((res) => {
          if (res.code == 0) {
            this.$message({
              type: 'success',
              message: '邀请成功'
            })
            this.init()
          } else {
            this.$message({
              type: 'error',
              message: res.error
            })
          }
        })
      } else {
        this.$message('请选择需要邀请的专家')
      }
    },
    getPatientInfo() {
      getPatientInfo(this.checkrowdata.patientId, this.checkrowdata.dispatchId).then(
        (res) => {
          this.userForm.idCard = res.idcardid
          this.userForm.tel = res.contactphone
        }
      )
    }
  }
}
</script>
